package Untils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Object;

/**
 * Created by Administrator on 2017/9/18.
 */
public class UserDao {
    public int pagesize = 3;

    public List<User> findPage(int page) throws SQLException {
        Connection con = C3P0Untils.getConetion();
        List<User> list = new ArrayList<>();
        String sql = "select * from user order by id desc limit ?,?";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, (page - 1) * pagesize);
        ps.setInt(2, pagesize);
        ResultSet set = ps.executeQuery();
        while (set.next()) {
            User user = new User();
            user.setId(set.getInt(1));
            user.setUsername(set.getString(2));
            user.setPhone(set.getString(3));
            list.add(user);
        }
        return list;
    }

    public List<User> findAll() throws SQLException {
        //执行SQL对象
        QueryRunner runner = new QueryRunner( C3P0Untils.getDataSources());
        String sql = "select * from user";
        //使用封装策略自动封装
        //结果集的封装策略：ResultSetHandle类
        //因为结果有不同的各类，可能是一个集合，也可能是一个对象

        List<User> list = runner.query(sql, new BeanListHandler<>(User.class));
        return list;
    }


    public User fingById(int id) {
        QueryRunner runner = new QueryRunner(C3P0Untils.getDataSources());
        String sql = "select * from user where id=?";
        try {
            User user = (User) runner.query(sql, new BeanHandler<>(User.class),id);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
       return null;
    }
    public Boolean insert(User user) throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Untils.getDataSources());
        String sql="insert into user(id,username,phone) values(?,?,?)";
        int num=  runner.update(sql,new Object[]{user.getId(),user.getUnname(),user.getPhone()});
        if(num>0)
        {
            return true;
        }
        return false;
    }
    public Boolean delete(int id) throws SQLException {
     QueryRunner queryRunner=new QueryRunner();
     String sql="delete from user where id = ?";
    int num = queryRunner.update(sql,id);
    if(num > 0)
    {
        return true;
    }
    else
        return false;
    }
    public Boolean  change(int username,int phone,int id) throws SQLException {
        QueryRunner runner=new QueryRunner(C3P0Untils.getDataSources());
        String sql="update user set username=?,phone=? where id=?";
        int num=runner.update(sql,username,phone,id);
        if(num>0)
        {
            return true;
        }
        else
            return false;
    }
}
